<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="WD">





<title>使用CMake构建Qt项目 | WD&#39;s blog</title>



    <link rel="icon" href="/favicon1.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    
    <script src="/js/snow.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 5.4.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">WD&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">WD&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">使用CMake构建Qt项目</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/about/">WD</a>
                     &nbsp;

                    
                        <span class="post-time">
                        Date: <a href="#">September 28, 2019&nbsp;&nbsp;16:48:13</a>
                        </span>
                     &nbsp;
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Cmake/">Cmake</a>
                            
                        </span>
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <br>
    <span id="busuanzi_container_site_pv">总阅读量:<a href="#"><span id="busuanzi_value_page_pv"></span></a>次</span>&nbsp;
    <span class="post-count">文章字数:<a href="#">978</span></a>&nbsp;
     <span class="post-count">阅读时长:<a href="#">3</span>min</a>
                    
                </div>
            
        </header>

        <div class="post-content">
            <ul>
<li>由于要做新的GUI设计加入更多的功能，在windows上已经安装了QtCreator，不想在Ubuntu下再装一个，于是采用在windows下开发，拿到Linux下编译运行，故使用CMake构建Qt项目。</li>
</ul>
<h2 id="1-Qt中的qmake"><a href="#1-Qt中的qmake" class="headerlink" title="1.Qt中的qmake"></a>1.Qt中的qmake</h2><ul>
<li>qmake可以利用源文件、头文件、qt的ui文件等生成各种不同类型的工程和工程需要的Makefile文件。</li>
<li>pro文件，添加各种源文件，还可以设定平台相关的不同源文件，设置各种规则，利用qmake命令生成工程。</li>
</ul>
<p>QT中编译三步走：</p>
<ul>
<li><strong>qmake -project</strong> (用于创建.pro文件，将所有的文件编译成一个与平台无关的工程文件)</li>
<li><strong>qmake</strong> (读取本身的Qt设置，生成与库一致的相应的Makefile)</li>
<li><strong>make</strong> (根据生成的Makefile，将文件编译为二进制可执行程序)   </li>
</ul>
<h2 id="2-CMake中的构建"><a href="#2-CMake中的构建" class="headerlink" title="2.CMake中的构建"></a>2.CMake中的构建</h2><ul>
<li><p>CMake中使用的参数代表指CMakeLists.txt文件所在的目录。这个CMakeLists.txt文件控制了整个编译的过程。为了更彻底地理解它，我们用以下这个图来看看整个编译的流程。下面这张图表明用户编写的文件（源代码，头文件，.ui文件，.qrc文件）在编译过程中是如何被Qt的工具进行处理，并整合到整个编译流程中的。因为qmake是用于处理这个流程的，它隐藏了这个流程中的很多细节。</p>
<p><img src="http://img.blog.csdn.net/20140630160126703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzI0MzcxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="img"></p>
</li>
<li><p>Qt 不是使用的“标准的” C++ 语言，而是对其进行了一定程度的“扩展”。这里我们从Qt新增加的关键字就可以看出来：signals、slots 或者 emit。所以有人会觉得 Qt 的程序编译速度慢，这主要是因为在 Qt 将源代码交给标准 C++ 编译器，如 gcc 之前，需要事先将这些扩展的语法去除掉。完成这一操作的就是 moc。</p>
</li>
</ul>
<ul>
<li>Qt中，如果一个类要使用信号/槽功能，就必须在其中声明Q_OBJECT。只有加入了Q_OBJECT，你才能使用QT中的signal和slot机制。</li>
</ul>
<p>  这时候，就必须在头文件派生类的时候，首先像下面那样引入Q_OBJECT宏：<br>  class MyMainWindow : public QWidget<br>     {<br>          Q_OBJECT<br>           ……<br>      }</p>
<ul>
<li><p>moc是QT的预编译器，用来处理代码中的slot，signal，emit，Q_OBJECT等。moc文件是对应的处理代码，也就是Q_OBJECT宏的实现部分</p>
</li>
<li><p>在头文件中如果有使用Q_OBJECT宏的话，则这个文件需要被moc进行处理，.ui文件也必须要由uic处理，.qrc文件需要由rcc程序处理。</p>
</li>
</ul>
<h2 id="3-具体构建过程"><a href="#3-具体构建过程" class="headerlink" title="3.具体构建过程"></a>3.具体构建过程</h2><ul>
<li>下面是我在windows下写的扫雷程序文件（包含.ui .qrc）</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/2019092816462333.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt=""></p>
<ul>
<li>下面是CMakeList.txt的内容。</li>
</ul>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#cmake版本</span></span><br><span class="line"><span class="keyword">cmake_minimum_required</span>(VERSION <span class="number">3.5</span>.<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#项目名称</span></span><br><span class="line"><span class="keyword">project</span>(Minesweeper)</span><br><span class="line"></span><br><span class="line"><span class="comment">#让cmake自动去寻找Qt5</span></span><br><span class="line"><span class="keyword">FIND_PACKAGE</span>(Qt5 COMPONENTS Widgets REQUIRED)</span><br><span class="line"></span><br><span class="line"><span class="comment">#采用C++11标准</span></span><br><span class="line"><span class="keyword">set</span>(CMAKE_CXX_STANDARD <span class="number">11</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#添加所有的源文件</span></span><br><span class="line"><span class="keyword">SET</span>(Minesweeper_SOURCES </span><br><span class="line">	dialog.cpp	</span><br><span class="line">	main.cpp </span><br><span class="line">	mainwindow.cpp </span><br><span class="line">	minemap.cpp</span><br><span class="line">	myitem.cpp</span><br><span class="line">	myscene.cpp</span><br><span class="line">	setpro.cpp</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">#添加所有的头文件</span></span><br><span class="line"><span class="keyword">SET</span>(Minesweeper_HEADERS </span><br><span class="line">	dialog.h</span><br><span class="line">	mainwindow.h</span><br><span class="line">	minemap.h</span><br><span class="line">	myitem.h</span><br><span class="line">	myscene.h</span><br><span class="line">	setpro.h</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">#添加所有的.ui文件</span></span><br><span class="line"><span class="keyword">SET</span>(Minesweeper_FORMS </span><br><span class="line">	dialog.ui</span><br><span class="line">	mainwindow.ui</span><br><span class="line">	setpro.ui</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">#添加资源文件</span></span><br><span class="line"><span class="keyword">SET</span>(Minesweeper_RESOURCES </span><br><span class="line">	img.qrc</span><br><span class="line">)</span><br><span class="line"><span class="comment">#调用预编译器moc，需要使用 QT5_WRAP_CPP宏</span></span><br><span class="line"><span class="keyword">QT5_WRAP_CPP</span>(Minesweeper_HEADERS_MOC <span class="variable">$&#123;Minesweeper_HEADERS&#125;</span>)</span><br><span class="line"><span class="comment">#使用uic处理.ui文件</span></span><br><span class="line">QT5_WRAP_UI(Minesweeper_FORMS_HEADERS <span class="variable">$&#123;Minesweeper_FORMS&#125;</span>)</span><br><span class="line"><span class="comment">#使用rcc处理.qrc文件</span></span><br><span class="line">QT5_ADD_RESOURCES(Minesweeper_RESOURCES_RCC <span class="variable">$&#123;Minesweeper_RESOURCES&#125;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#这些生成的中间文件都会在build目录下，这样的话，编译器则不能定位由uic程序产生的诸如_ui_mainwindow.h等文件。所以，我们需要把build目录添加到包含目录中</span></span><br><span class="line"><span class="keyword">INCLUDE_DIRECTORIES</span>(<span class="variable">$&#123;CMAKE_CURRENT_BINARY_DIR&#125;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#生成可执行文件</span></span><br><span class="line"><span class="keyword">ADD_EXECUTABLE</span>(Minesweeper </span><br><span class="line">	<span class="variable">$&#123;Minesweeper_SOURCES&#125;</span> </span><br><span class="line">	<span class="variable">$&#123;Minesweeper_HEADERS_MOC&#125;</span> </span><br><span class="line">    <span class="variable">$&#123;Minesweeper_FORMS_HEADERS&#125;</span></span><br><span class="line">	<span class="variable">$&#123;Minesweeper_RESOURCES_RCC&#125;</span> </span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">#为target添加需要链接的共享库</span></span><br><span class="line"><span class="keyword">TARGET_LINK_LIBRARIES</span>(Minesweeper <span class="variable">$&#123;Qt5Widgets_LIBRARIES&#125;</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>下面是build的结果：</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20190928164731554.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt=""></p>
<ul>
<li>下面是运行之后的结果：</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20190928164742975.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt=""></p>
<h2 id="4-结尾"><a href="#4-结尾" class="headerlink" title="4.结尾"></a>4.结尾</h2><ul>
<li>有了上面的构建，也可以完成其他资源的添加，结合新的底层就可以开发出好的界面和功能了。</li>
</ul>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span><a href="/about/">WD</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://did321.gitee.io/2019/09/28/%E4%BD%BF%E7%94%A8CMake%E6%9E%84%E5%BB%BAQt%E9%A1%B9%E7%9B%AE/">https://did321.gitee.io/2019/09/28/%E4%BD%BF%E7%94%A8CMake%E6%9E%84%E5%BB%BAQt%E9%A1%B9%E7%9B%AE/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>Copyright (c) 2022 <a target="_blank" rel="noopener" href="http://creativecommons.org/licenses/by-nc/4.0/">CC-BY-NC-4.0</a> LICENSE</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span><a href="#">The blog is my giant.</a></span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/Cmake/"># Cmake</a>
                    
                        <a href="/tags/Qt/"># Qt</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2019/09/29/FPGA%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/">可编程器件应用FPGA——学习笔记(一)</a>
            
            
            <a class="next" rel="next" href="/2019/08/26/%E3%80%8A%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E3%80%8B%E8%A7%A3%E6%9E%90%E3%80%8A%E5%8D%97%E7%93%9C%E4%B9%A6%E3%80%8B%E9%A1%B9%E7%9B%AE%E9%93%BE%E6%8E%A5/">《机器学习》解析《南瓜书》项目链接</a>
            
        </section>
        <br>
        <br>
    
    <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
    <div id="vcomments"></div>
    <script>
        new Valine({
    el: '#vcomments' ,

    appId: 'JvFy3ebVLo2rUYgHaMweJyXX-MdYXbMMI',
    appKey: 'TCFxfjDAM8UmERPEgYXJmT40',
    serverURLs: 'https://JvFy3ebV.api.lncldglobal.com', 
    placeholder: '----评论区----留下你的评论，作者会定期回复！在昵称处填写QQ号可自动获取邮箱和QQ头像（保护QQ邮箱隐私）',
    enableQQ: true,
    requiredFields: ['nick'],
});
    </script>

    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© WD | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>

</html>
